package com.theobserver.theobserver;

import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

//import com.avos.avoscloud.AVAnalytics;
import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.SaveCallback;


public class FeedBackActivity extends AppCompatActivity{
    private Handler mHandler = new Handler();
    private ProgressBar mProgerss;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);

        mProgerss = (ProgressBar) findViewById(R.id.mProgess);

        TextView mreturn = (TextView)findViewById(R.id.TextView_feedback_return);
        mreturn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view){
                finish();
            }
        });

        final EditText memailEdit = (EditText) findViewById(R.id.TextView_feedback_email);
        final EditText mcontentEdit = (EditText) findViewById(R.id.TextView_feedback_feedback);

        findViewById(R.id.button_feedback_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("".equals(mcontentEdit.getText().toString())) {
                    Toast.makeText(FeedBackActivity.this, "输入反馈", Toast.LENGTH_SHORT).show();
                    return;
                }
                if ("".equals(memailEdit.getText().toString())) {
                    Toast.makeText(FeedBackActivity.this, "输入邮箱", Toast.LENGTH_SHORT).show();
                    return;
                }
                mProgerss.setVisibility(View.VISIBLE);
                AVObject comment = new AVObject("feedback");

                comment.put("feedback_content", mcontentEdit.getText().toString());
                comment.put("feedback_email", memailEdit.getText().toString());

                comment.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(AVException e) {
                        if (e == null) {
                            mProgerss.setVisibility(View.GONE);
                            FeedBackActivity.this.finish();
                        } else {
                            mProgerss.setVisibility(View.GONE);
                            Toast.makeText(FeedBackActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onPause() {
        super.onPause();
        //AVAnalytics.onPause(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //AVAnalytics.onResume(this);
    }
}
